$().ready(function() {
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {

	$.ajax({
		cache : true,
		type : "POST",
		url : "/mall/advert/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				if (data.msg) {
					parent.layer.alert(data.msg)
				}else {
					window.location.href = "/";
				}
			}

		}
	});
}

function onThumbBlur() {
	var url = $.trim($('#thumb').val());
	if (url) {
		if (url.indexOf("http") >= 0 || url.indexOf("https") >= 0) {
			$('#thumbImg').attr('src', url);
		}else {
			var prefix = "http://pic.jinzhishenghuo.com";
			$('#thumbImg').attr('src', prefix + url);
		}
	}
}

/**
 * 分类选择变更
 * @param category
 */
function categoryChange(category) {
	var categoryId = category.value;
    var categoryLinkPrefix = $('#categoryLinkPrefix').val();
	if (categoryId) {
		$.ajax({
			url:"/category/manage/getCategoryDetail",
			type:"POST",
			data:{
				categoryId: categoryId
			},
			dataType:'JSON',
			error : function(request) {
				parent.layer.alert("Connection error");
			},
            success:function (data) {
                if (data.code === 0) {
                    var category = data.category;
                    if (category) {
                        //$('#link').val(categoryLinkPrefix+ "category="+category.name +"&itemClass="+ category.id );
                    	$('#link').val(categoryLinkPrefix+"itemClass="+ category.id );
                    }
                }
            }
		})
	}
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			advname : {
				required : true
			},
			thumb : {
				required : true
			}
		},
		messages : {
			advname : {
				required : icon + "广告标题不能为空"
			},
			thumb : {
				required : icon + "广告图片不能为空"
			}
		}
	})
}

/**
 * 加载图片URL
 * @param inputId
 * @param imgId
 * @param url
 */
function loadImgUrl(inputId, imgId, url) {
	$('#'+inputId +'').val(url);
	$('#'+imgId +'').attr('src', url);
}

/**
 * 选择图片页面
 * @param inputId
 * @param imgId
 */
function onSelectImgBtnClick(inputId, imgId) {
	layer.open({
		id:'selectImg',
		type : 2,
		title : '选择图片',
		maxmin : true,
		shadeClose : false,
		area : [ '60%', '70%' ],
		content : '/platform/config/selectImg/' + inputId +"/"+ imgId // iframe的url
	});
}

/**
 * 链接到商品
 * @param thumb
 * @param title
 */
function linkProduct(id, title) {
    /*var productLinkPrefix = $('#productLinkPrefix').val();*/
   /* $('#link').val(productLinkPrefix+ id);*/
	$('#link').val(id);
    $('#productName').val(title);
}

function linkToProduct() {
    var centreId = $("*[name='centreId']").val();
    layer.open({
        id: 'linkToProduct',
        type: 2,
        title: '链接到商品',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['60%', '80%'],
        content:  '/mall/advert/linkToProduct?centreId='+ centreId // iframe的url
    });
}